package cn.com.shopec.erp.product.controller;

import java.util.List;

import javax.annotation.Resource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSONObject;
import cn.com.shopec.erp.base.service.ISupplierInfoService;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.JsonHelper2;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.financing.service.IWhareHouseServer;
import cn.com.shopec.erp.product.entity.Defective;
import cn.com.shopec.erp.product.entity.ProductStockExt;
import cn.com.shopec.erp.product.service.ICommodityService;
import cn.com.shopec.erp.product.service.IDefectiveService;
import cn.com.shopec.erp.product.service.IProductService;
import cn.com.shopec.erp.product.service.IProductStockService;
import cn.com.shopec.erp.product.vo.CommodityVo;
import cn.com.shopec.erp.product.vo.DefectiveVo;
import cn.com.shopec.erp.product.vo.ProductStockVo;
import cn.com.shopec.erp.system.common.UserUtils;
import cn.com.shopec.erp.system.service.IUserService;
import cn.com.shopec.erp.system.vo.RoleVo;
import net.sf.json.JSONArray;

/**
 * @ClassName: DefectiveController
 * @Description: 次品列表controller
 * @author: LiangShunWang
 * @date: 2016-11-21 下午16:18:13
 */
@Controller
@RequestMapping("/product/defectivemgt")
public class DefectiveController {

	private static Log log = LogFactory.getLog(CommodityCatController.class);
	@Resource
	private IDefectiveService defectiveService;

	@Resource
	private IUserService userService;

	@Resource
	private ICommodityService commodityService;

	@Resource
	private IWhareHouseServer iWhareHouseServer;

	@Resource
	private ISupplierInfoService supService;

	@Resource
	private IProductService productService;

	@Resource
	private IProductStockService productStockService;

	/**
	 * 
	 * @Title: toListDefective
	 * @Description: 跳转到次品列表页面
	 * @param brandVo
	 * @param map
	 * @return
	 * @throws Exception
	 * @throws @author:LiangShunWang
	 * @date: 2016-11-21下午16:31:46
	 */
	@RequestMapping("/to_list_defective")
	public String toListDefective(DefectiveVo defectiveVo, ModelMap map) {
		map.put("defective", defectiveVo);
		return "/product/defective_list";
	}

	/**
	 * 
	 * @Title: listBrand
	 * @Description: 分页查询次品信息
	 * @param brandVo
	 * @param pageQuery
	 * @param map
	 * @return
	 * @throws @author:
	 *             LiangShunWang
	 * @date: 2013-5-27下午01:33:30
	 */
	@RequestMapping("/list_defective")
	@ResponseBody
	public String listDefective(DefectiveVo defectiveVo, PageQuery pageQuery, ModelMap map) {
		try {
			PageFinder<DefectiveVo> pagedBrandList = this.defectiveService.getPagedBrandList(defectiveVo, pageQuery);
			map.put("Rows", pagedBrandList.getData());
			map.put("Total", pagedBrandList.getRowCount());
		} catch (Exception e) {
			log.error("listBrand:" + e.getMessage(), e);
		}
		return JsonHelper2.toJson(map);

	}

	/**
	 * 
	 * @Title: toAddBrand
	 * @Description: 跳转到添加次品页面
	 * @return
	 * @throws @author:LiangShunWang
	 * @date: 2016-11-21下午19:21:57
	 */
	@RequestMapping("/to_add_defective")
	public String toAddDefective(PageQuery pageQuery, ModelMap map) {
		map.put("whareHouse", iWhareHouseServer.getPagedCollectSetList(pageQuery).getData());
		map.put("supplier", supService.getPagedCollectSetList(pageQuery).getData());
		return "/product/defective_add";
	}

	/**
	 * @Title: addBrand
	 * @Description: 添加次品信息
	 * @param brandVo
	 * @param map
	 * @return
	 * @throws Exception
	 * @throws @author: LiangShunWang
	 * @date: 2016-11-21下午19:16:58
	 */
	@RequestMapping("/add_defective")
	@ResponseBody
	public String addDefective(DefectiveVo defectiveVo, String defectives, ModelMap map) {
		try {
			JSONArray jsonArray = JSONArray.fromObject(defectives);
			@SuppressWarnings("unchecked")
			List<Defective> stockInProducts = (List<Defective>) JSONArray.toList(jsonArray, Defective.class);
			this.defectiveService.addDefective(stockInProducts);
		} catch (Exception e) {
			log.error("addBrand:" + e.getMessage(), e);
			return SZSCMConstant.FAIL;
		}
		return SZSCMConstant.SUCCESS;
	}

	/**
	 * @Title: toUpdateBrand
	 * @Description: 跳转到修改次品页面
	 * @param brandVo
	 * @param modelMap
	 * @return
	 * @throws @author:LiangShunWang
	 * @date: 2013-5-27下午02:43:02
	 */
	@RequestMapping("/to_update_defective")
	public String toUpdateDefective(String defectiveId, ModelMap map) {
		try {
			DefectiveVo defective = this.defectiveService.getDefective(defectiveId);
			map.put("defective", defective);
		} catch (Exception e) {
			log.error("toUpdateBrand:" + e.getMessage(), e);
		}
		return "/product/defective_update";
	}

	/**
	 * 
	 * @Title: updateBrand
	 * @Description: 修改次品信息
	 * @param brandVo
	 * @param map
	 * @return
	 * @throws @author:LiangShunWang
	 * @date: 2013-5-27下午02:48:51
	 */
	@RequestMapping("/update_defective")
	@ResponseBody
	public String updateDefective(DefectiveVo defectiveVo, ModelMap map) {
		try {
			if (this.defectiveService.updateDefective(defectiveVo)) {
				return SZSCMConstant.SUCCESS;
			}
		} catch (Exception e) {
			log.error("update_defective:" + e.getMessage(), e);
		}
		return SZSCMConstant.FAIL;
	}

	/**
	 * 
	 * @Title: deleteDefective
	 * @Description: 删除次品
	 * @param brandVo
	 * @return
	 * @throws @author: LiangShunWang
	 * @date: 2013-6-6下午20:48:51
	 */
	@RequestMapping("deleteDefective")
	@ResponseBody
	public String deleteBrand(DefectiveVo DefectiveVo) {
		try {
			this.defectiveService.delDefective(DefectiveVo.getId());
			return SZSCMConstant.SUCCESS;
		} catch (Exception e) {
			log.error("deleteBrand:" + e.getMessage(), e);
			return SZSCMConstant.FAIL;
		}
	}

	/**
	 * 跳至商品列表
	 * 
	 * @param map
	 * @return
	 */
	@RequestMapping("toProductList")
	public String toPageCommodityList(CommodityVo commodityVo, PageQuery pageQuery, ModelMap map) throws Exception {
		List<RoleVo> roleVos = this.userService.getOwnRoleVos(UserUtils.getUserVo());
		map.put("roleVos", roleVos);
		map.put("commodity", commodityVo);
		return "/product/spec/commodity_list";
	}

	/**
	 * 分页查询商品列表
	 * 
	 * @param commodity1
	 * @param pageQuery
	 * @param map
	 * @return
	 */
	@RequestMapping({ "aJaxPageCommodityList" })
	@ResponseBody
	public String aJaxPageCommodityList(CommodityVo commodityVo, PageQuery pageQuery, ModelMap map) {
		JSONObject jsonObj = new JSONObject();

		try {
			PageFinder<CommodityVo> pageFinder = this.commodityService.pageFinderCommodity(commodityVo, pageQuery);
			jsonObj.put("Rows", pageFinder.getData());
			jsonObj.put("Total", pageFinder.getRowCount());
		} catch (Exception e) {
			log.error("aJaxPageCommodityList:" + e);
		}
		return jsonObj.toJSONString();
	}

	/**
	 * @methodName：getCommodityList
	 * @Description: 进入选择商品页面
	 * @param ProductVo pageQuery
	 * @return
	 * @author: liang shun wang
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("/getCommodityList")
	public ModelAndView getCommodityList(ProductStockVo productStockVo, boolean selectOnlyRow, PageQuery pageQuery,
			ModelMap model) throws Exception {
		model.put("productStockVo", productStockVo);
		model.put("selectOnlyRow", selectOnlyRow);
		return new ModelAndView("product/spec/product_elect", model);
	}

	/**
	 * @methodName：getAjaxCommodityList
	 * @Description: 获取商品
	 * @param ProductVo pageQuery
	 * @return
	 * @author: gaoyaxiong
	 * @throws Exception
	 * @date: 2013-5-22 下午15:10:03
	 */
	@RequestMapping("/getAjaxCommodityList")
	@ResponseBody
	public String productStockPageList(ProductStockVo productStockVo, PageQuery pageQuery) {
		PageFinder<ProductStockExt> page = productStockService.productStockDefect(productStockVo, pageQuery);
		ModelMap map = new ModelMap();
		map.put("Rows", page.getData());
		map.put("Total", page.getRowCount());
		return JsonHelper2.toJson(map);
	}

	/**
	 * 审核
	 *
	 * @param defectiveId
	 * @return
	 */
	@RequestMapping("/defectiveAduit")
	@ResponseBody
	public String aduitDefective(String defectiveId) {
		try {
			if (this.defectiveService.aduitDefective(defectiveId)) {
				return SZSCMConstant.SUCCESS;
			}

		} catch (Exception e) {
			log.error("deleteBrand:" + e.getMessage(), e);
		}
		return SZSCMConstant.FAIL;
	}

	/**
	 * @Title: showPoDetail
	 * @Description: 跳转展现次品详情页面
	 * @param poNo
	 * @return
	 * @throws Exception
	 * @throws @author:liang shun wang
	 * @date: 2013-5-27下午2:33:39
	 */
	@RequestMapping("showPoDetail")
	public ModelAndView showPoDetail(String id, String showList, ModelMap modelMap) throws Exception {
		// PurchaseOrderVo po = this.poService.getPurchaseOrder(poNo);
		DefectiveVo po = this.defectiveService.getDefective(id);
		modelMap.put("po", po);
		// modelMap.put("num", num);
		modelMap.put("showList", StringUtil.isBlank(showList) ? "prods" : showList);
		return new ModelAndView("product/defective_detail");
	}

	/**
	 * @Title: ajaxToPoProductList
	 * @Description: 根据采购单号，取得采购单的货品信息，以json方式返回
	 * @param poNo
	 * @param pageQuery
	 * @param modelMap
	 * @return
	 * @throws Exception
	 * @throws @author:liang shun wang
	 * @date: 2013-5-27下午3:14:28
	 */
	@RequestMapping("ajaxToPoProductList")
	@ResponseBody
	public String ajaxToPoProductList(String id, PageQuery pageQuery, ModelMap modelMap) throws Exception {
		JSONObject jsonObj = new JSONObject();
		DefectiveVo defectiveVo = new DefectiveVo();
		defectiveVo.setId(id);
		PageFinder<DefectiveVo> pageFinder = this.defectiveService.getPagedBrandList(defectiveVo, pageQuery);
		jsonObj.put("Rows", pageFinder.getData());
		jsonObj.put("Total", pageFinder.getRowCount());
		return jsonObj.toJSONString();
	}
	
	
	//-------------------------
	
	/**
	 * 
	 * @Title: toListDefective
	 * @Description: 跳转到次品历史列表页面
	 * @param brandVo
	 * @param map
	 * @return
	 * @throws Exception
	 * @throws @author:LiangShunWang
	 * @date: 2016-11-21下午16:31:46
	 */
	@RequestMapping("/to_list_defective_history")
	public String toListHistoryDefective(DefectiveVo defectiveVo, ModelMap map) {
		map.put("defective", defectiveVo);
		map.put("isHistoryPage", true);
		return "/product/defective_history_list";
	}

	/**
	 * 
	 * @Title: listBrand
	 * @Description: 分页查询次品转换信息
	 * @param brandVo
	 * @param pageQuery
	 * @param map
	 * @return
	 * @throws @author:
	 *             LiangShunWang
	 * @date: 2013-5-27下午01:33:30
	 */
	@RequestMapping("/list_history_defective")
	@ResponseBody
	public String listHistoryDefective(DefectiveVo defectiveVo, PageQuery pageQuery, ModelMap map) {
		try {
			PageFinder<DefectiveVo> pagedBrandList = this.defectiveService.getPagedBrandHistoryList(defectiveVo, pageQuery);
			map.put("Rows", pagedBrandList.getData());
			map.put("Total", pagedBrandList.getRowCount());
		} catch (Exception e) {
			log.error("listBrand:" + e.getMessage(), e);
		}
		return JsonHelper2.toJson(map);

	}

}